/**
 * 定义多个可以间接更改state数据的方法  本质核心是通过commit 去触发mutations里面的方法
 * 
 */

import { RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS ,
    RECEIVE_USER_INFO,
    RESET_USER_INFO
} from './mutation-types'

import  {reqAddress,reqCategorys,reqShops,reqUser} from '../api/index'

export default {
    //5k
     async   getAddress(context) { 
        let geohash=context.state.latitude+','+context.state.longitude
        const result=    await reqAddress(geohash)
        if(result.code===0) {
            let obj={address:result.data}
            
         context.commit(RECEIVE_ADDRESS,obj)
      
        }   
    },


    async  getCategorys(context) {
        const result=  await reqCategorys()
        if(result.code===0)  {
            let obj={categorys:result.data}


           
            context.commit(RECEIVE_CATEGORYS,obj)

           
        }

    },
 
 

    async  getShops(context) {
        let aaa={latitude:context.state.latitude,longitude:context.state.longitude}
        const result=  await reqShops(aaa)
        if(result.code===0)  {
            let obj={shops:result.data}
            context.commit(RECEIVE_SHOPS,obj)
        }
       
    },






    // async   getAddress({commit,state}) {
    
    //     let geohash=state.latitude+','+state.longitude
    //     const result=    await reqAddress(geohash)
    //     if(result.code===0)  commit(RECEIVE_ADDRESS,{address:result.data})

    // },



    //   async  getCategorys({commit}) {
    //     const result=    await reqCategorys()
    //     if(result.code===0)  commit(RECEIVE_CATEGORYS,{categorys:result.data})



    // },

    // async  getShops({commit,state}) {
    //     let{latitude,longitude}={state}
    //     const result=  await reqShops({latitude,longitude})
    //     if(result.code===0)  commit(RECEIVE_SHOPS,{shops:result.data})
       
    // }


   

    // 异步获取用户信息

  async getUserInfo({commit}){

     let result=  await reqUser()
     console.log(result)
     if(result.code===0) {
        commit(RECEIVE_USER_INFO,{userInfo: result.data})
     }
    
},



     //记录用户信息
     recordUserInfo({commit}){




        commit(RECEIVE_USER_INFO,{userInfo})
    }

}